package com.example.demo.controller;

import com.example.demo.dto.TaskTypeDTO;
import com.example.demo.entity.TaskType;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.TaskTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/task-types")
public class TaskTypeController {

    @Autowired
    private TaskTypeService taskTypeService;

    @PostMapping
    public ResponseEntity<ApiResponse<TaskTypeDTO>> createTaskType(
            @RequestBody TaskType taskType,
            HttpServletRequest request) {
        try {
            TaskType createdTaskType = taskTypeService.createTaskType(taskType);
            TaskTypeDTO taskTypeDTO = TaskTypeDTO.fromEntity(createdTaskType);
            return ResponseEntity.ok(
                    ApiResponse.created(taskTypeDTO, "创建成功", request.getRequestURI()));
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.fail(400, e.getMessage(), request.getRequestURI()));
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteTaskType(@PathVariable("id") Long taskTypeId,
            HttpServletRequest request) {
        boolean deleted = taskTypeService.deleteTaskType(taskTypeId);
        if (deleted) {
            return ResponseEntity.ok(ApiResponse.success(null, "删除成功", request.getRequestURI(), null));
        } else {
            return ResponseEntity.status(404)
                    .body(ApiResponse.fail(404, "任务类型未找到", request.getRequestURI()));
        }
    }

    @PutMapping
    public ResponseEntity<ApiResponse<TaskTypeDTO>> updateTaskType(
            @RequestBody TaskType taskType,
            HttpServletRequest request) {
        TaskType updatedTaskType = taskTypeService.updateTaskType(taskType);
        TaskTypeDTO taskTypeDTO = TaskTypeDTO.fromEntity(updatedTaskType);
        return ResponseEntity.ok(
                ApiResponse.success(taskTypeDTO, "更新成功", request.getRequestURI(), null));
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<TaskTypeDTO>> getTaskTypeById(@PathVariable("id") Long taskTypeId,
            HttpServletRequest request) {
        TaskTypeDTO taskTypeDTO = taskTypeService.getTaskTypeById(taskTypeId);
        if (taskTypeDTO != null) {
            return ResponseEntity.ok(
                    ApiResponse.success(taskTypeDTO, "查询成功", request.getRequestURI(), null));
        } else {
            return ResponseEntity.status(404)
                    .body(ApiResponse.fail(404, "任务类型未找到", request.getRequestURI()));
        }
    }

    @GetMapping
    public ResponseEntity<ApiResponse<List<TaskTypeDTO>>> getAllTaskTypes(HttpServletRequest request) {
        List<TaskTypeDTO> taskTypes = taskTypeService.getAllTaskTypes();
        return ResponseEntity.ok(
                ApiResponse.success(taskTypes, "查询成功", request.getRequestURI(), null));
    }

    /**
     * 根据数据源ID查询任务类型列表
     */
    @GetMapping("/data-source/{dataSourceId}")
    public ResponseEntity<ApiResponse<List<TaskTypeDTO>>> getTaskTypesByDataSourceId(
            @PathVariable("dataSourceId") Long dataSourceId,
            HttpServletRequest request) {
        List<TaskTypeDTO> taskTypes = taskTypeService.getTaskTypesByDataSourceId(dataSourceId);
        return ResponseEntity.ok(
                ApiResponse.success(taskTypes, "查询成功", request.getRequestURI(), null));
    }

    /**
     * 根据任务名称搜索任务类型
     */
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<TaskTypeDTO>>> searchTaskTypesByName(
            @RequestParam("name") String taskName,
            HttpServletRequest request) {
        List<TaskTypeDTO> taskTypes = taskTypeService.searchTaskTypesByName(taskName);
        return ResponseEntity.ok(
                ApiResponse.success(taskTypes, "搜索成功", request.getRequestURI(), null));
    }

    /**
     * 统计任务类型总数
     */
    @GetMapping("/count")
    public ResponseEntity<ApiResponse<Integer>> countTaskTypes(
            HttpServletRequest request) {
        int count = taskTypeService.countTaskTypes();
        return ResponseEntity.ok(
                ApiResponse.success(count, "统计成功", request.getRequestURI(), null));
    }
}